# -*- coding:utf-8 -*-
"""
该脚本是对当前maya工程中的相机进行操作
"""
try:
    import maya.cmds as cmds
except Exception as test_ERR:
    print('test')


def camera_check():

    sobj = cmds.ls(sl=1,l=1)
    obj_transform = sobj[0] if sobj else None
    shape = cmds.listRelatives(sobj)[0]
    a = cmds.listAttr(shape, st='lensSqueezeRatio')
    if a:
        return True,obj_transform,shape
    else:
        return False,obj_transform,shape



def set_main_camera(asset_shot_name):
    """
    对选择的相机设置名称
    :param asset_shot_name : 主相机名称与资产与镜头名称一致
    :return:
    """
    iscam, obj_transform, shape = camera_check()
    if iscam:
        cameras = cmds.ls(type='camera', l=1)
        for i in cameras:
            split_shape = i.split('|')
            transform = split_shape[-2]
            new_slpit = split_shape[:-1]
            long_name = '|'.join(new_slpit)  # get long name
            if transform == asset_shot_name:
                cmds.rename(long_name, 'camera')
        cmds.rename(obj_transform, asset_shot_name)
        shape = cmds.listRelatives(asset_shot_name)[0]
        cmds.rename(shape, asset_shot_name+'Shape1')
        cmds.confirmDialog(title=u'设置主相机', message=u'主相机名称为:\n{}'.format(asset_shot_name),
                                    button=['Yes'], defaultButton='Yes')
    return iscam



def get_main_camera(camera_name):
    """
    获取主相机
    :param camera_name: 资产或镜头名称
    :return: 返回的是shape
    """
    main_camera = None
    if camera_name:
        cameras = cmds.ls(type='camera')
        for c in cameras:
            if camera_name in c:
                main_camera = c
    return main_camera



def get_camera_info():
    """
    获取相机信息
    :return:
    """
    pass


def get_camera_focal(camera_name):
    """
    获取焦距
    :param camera_name: 相机shape名称
    :return:
    """
    fl = cmds.getAttr(camera_name + '.focalLength')
    return fl


def get_main_camera_focal(asset_shot_name):
    fl = 0
    main_camera = get_main_camera(asset_shot_name)
    if main_camera:
        fl = cmds.getAttr(main_camera + '.focalLength')

    return fl


def get_camera_BGimg(camera_transform):
    """
    查询相机的背景图路径
    :param camera_transform: 大纲视图显示名称
    :return:
    """
    BG_path = ''
    result = cmds.listConnections(cmds.listRelatives(camera_transform),type='imagePlane')
    #print(result)     #[u'CXRX_pianhua_001Shape1->imagePlane1']
    if result:
        shapes = cmds.listRelatives(result)
        BG_path = cmds.getAttr(shapes[0]+'.imageName')

    return BG_path

